<?php
ob_start(); //输出控制
/**
* HTTPS 
*/
function curl_post($url, $data, $header, $post = 1) {
        //初始化 curl
        $ch = curl_init();
        //设置选项
        $res = curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_POST, $post);
        if ($post)
                curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        $result = curl_exec($ch);
        //解析数据
        if ($result == FALSE) {
                if ($this->BodyType == 'json') {
                        $result = "{\"statusCode\":\"172001\",\"statusMsg\":\"money need\"}";
                } else {
                        $result = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><Response><statusCode>172001</statusCode><statusMsg>money need</statusMsg></Response>";
                }
        }

        curl_close($ch);
        return $result;
}

$BodyType = "json"; //指定body类型 json,x-www-form-urlencoded,xml
$data = array('from' => '18518488785', 'to' => '13141241232');
//设置body
 if ($BodyType == "json") {
         $body = json_encode($data, 256);
 } else {
         $body = http_build_query($data);;
 }

 //生成签名
 $sig = strtoupper(md5('jiangyong' . 'token' . date("YmdHis")));

 $url = "http://localhost/phponly/url/response.php?sig=$sig";

 $authen = base64_encode('jiangyong' . ":" . date("YmdHis"));
 $header = array("Accept:application/$BodyType", "Content-Type:application/$BodyType;charset=utf-8", "Authorization:$authen");

 //请求
 $result = curl_post($url, $body, $header);
 echo $result;
//ob_end_flush();
     
